package com.wiley.service;

import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.wiley.publicMethod.BasePage;
import com.wiley.publicMethod.LoggerControler;

import java.net.MalformedURLException;

public class DeliveryAddressPage extends BasePage {

	/**
	 * 页面元素
	 */
	//我的
	public static final By mine_item_loc = By.id(BaseId + "mine_item");

	//点击登录按钮
	public static final By login_tv_loc = By.id(BaseId + "login_tv");

	//登录界面：用户
	public static final By login_user_loc = By.id(BaseId+"login_user");

	//登录界面：密码
	public static final By login_password_loc = By.id(BaseId + "login_password");

	//登录界面：登录好豆
	public static final By login_haodou_loc = By.id(BaseId+"login_haodou");
	
	//用户昵称
	public static final By nick_tv_loc = By.id(BaseId+"nick_tv");
	
	//收货地址
	public static final By delivery_address_loc = By.id(BaseId+"deliveryAddress");
	
	//添加收货地址
	public static final By add_address_loc = By.id(BaseId+"add_address");
	
	public static final By add_address_opt_loc = By.name("添加收货地址");
	
	//收货地址管理
	public static final By address_manage_loc = By.name("收货地址管理");
	
	//姓名 address_name
	public static final By address_name_loc = By.id(BaseId+"address_name");
	
	//联系电话 address_phone
	public static final By address_phone_loc = By.id(BaseId+"address_phone");
	
	//省/市 city_name_tv
	public static final By city_name_tv_loc = By.id(BaseId+"city_name_tv");
	
	//layoutCity
	public static final By layoutCity_loc = By.id(BaseId+"layoutCity");
	
	// 省
	public static final By province_loc = By.id(BaseId+"provinceView");
	
	// 市
	public static final By city_loc = By.id(BaseId+"cityView");
	
	// btnSubmit
	public static final By submit_btn_loc = By.id(BaseId+"btnSubmit");
	
	//行政区/街道 address_full
	public static final By address_full_loc = By.id(BaseId+"address_full");
	
	// tv_location_list_item_name
	public static final By list_item_name_loc = By.id(BaseId+"tv_location_list_item_name");
	
	//详细地址 address_full_edit
	public static final By address_full_edit_loc = By.id(BaseId+"address_full_edit");
	
	//设置为默认地址 address_set_default_layout
	public static final By address_set_default_layout_loc = By.id(BaseId+"address_set_default_layout");
	
	//保存
	public static final By save_bt_loc = By.id(BaseId+"button");
	
	/**
	 * 操作方法
	 */
	//点击我的
	public void clickMineItem(){
		this.clickEle(mine_item_loc);
	}

	//登录按钮
	public void clickLoginButton(){
		this.clickEle(login_tv_loc);
	}

	//定位输入框：用户
	public void inputLoginUser(String username){
		this.sendKeys(login_user_loc,username);
	}
	
	//定位输入框：密码
	public void inputLoginPwd(String pwd){
		this.sendKeys(login_password_loc,pwd);
	}

	//点击登录好豆
	public void clickLoginHaoDou(){
		this.clickEle(login_haodou_loc);
	}

	//用户昵称
	public String getUserName(){
		WebElement ele = this.findEle(nick_tv_loc);
		return ele.getText();
	}
	
	//点击收货地址
	public void clickDeliveryAddress(){
		this.clickEle(delivery_address_loc);
	}
	
	//点击添加收货地址
	public void clickAddAddress(){
		if(super.isElementPresent(address_manage_loc)){
			this.clickEle(add_address_opt_loc);
		}else{
			this.clickEle(add_address_loc);
		}
	}
	
	//输入姓名
	public void inputAddressName(String addressName){
		this.sendKeys(address_name_loc, addressName);
	}
	
	//输入联系电话
	public void inputAddressPhone(String addressPhone){
		this.sendKeys(address_phone_loc, addressPhone);
	}
	
	//点击省/市
	public void clickCity(){
		this.clickEle(city_name_tv_loc);
		this.clickEle(submit_btn_loc);
	}
	
	//点击行政区/街道
	public void clickDistrictOrStreet(){
		try{
			this.clickEle(address_full_loc);
			this.clickEles(list_item_name_loc, 2);
		}catch(Exception e){
			driver.sendKeyEvent(4);
			this.clickEle(address_full_loc);
			this.clickEles(list_item_name_loc, 2);
		}
	}
	
	//输入详细地址
	public void inputDetailAddress(String detailAddress){
		this.sendKeys(address_full_edit_loc, detailAddress);
	}
	
	//设置为默认地址
	public void setDefaultAddress(){
		this.clickEle(address_set_default_layout_loc);
	}
	
	//点击保存
	public void clickSave(){
		this.clickEle(save_bt_loc);
	}

	
	public void addDeliveryAddress() {
		super.swipeToUp(1000);
		this.clickDeliveryAddress();
		this.clickAddAddress();
		this.inputAddressName("范国伟");
		this.inputAddressPhone("18250862110");
		this.clickCity();
		this.clickDistrictOrStreet();
		this.inputDetailAddress("一层尚科网络");
		this.setDefaultAddress();
		this.clickSave();
	}
	/**
	 * 业务封装
	 * @throws MalformedURLException 
	 */
	//添加地址
	public void addressFunc(String username,Class name) throws MalformedURLException {
		LoggerControler log = this.MyLogger(name);
		AndroidDriver driver = super.SetDriver();
		this.clickMineItem();
		if(super.isElementPresent(nick_tv_loc)){ //判断是否是已登录状态
			this.addDeliveryAddress();
			log.info("该case测试通过");
		}else{
			this.clickLoginButton();
			this.inputLoginUser(username);
			log.info("本次输入的账号是："+username);
			this.inputLoginPwd("fgwvip123");
			this.clickLoginHaoDou();
			if(super.isElementPresent(nick_tv_loc)){  //判断是否登录成功
				this.addDeliveryAddress();
				log.info("该case测试通过");
			}
		}
	}
}
